var express = require('express');
var conn = require('../public/javascripts/connect');
var judge_login = require('../public/javascripts/judge_login');
var router = express.Router();

router.get("/",async function (req, res){
    let user={};
    let Jurisdiction=null;
    if(req.cookies.jinshan_rabbing){
        Jurisdiction=await judge_login.judgelogin(req.cookies.jinshan_rabbing);
        console.log("Jurisdiction",Jurisdiction);
        if(Jurisdiction){
            user.department_number=Jurisdiction.department_number;
            user.real_name=Jurisdiction.real_name;
        }
    };
    if(Jurisdiction){
        var titles = [];
        var sql1 = "SELECT * FROM analysis_conditions WHERE 创建人 = '" + user.real_name + "'";
        conn.connection2.query(sql1, function (err, result) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            }
            for (var i in result) {
                titles.push(result[i]['标题'])
            }
            user.titles = JSON.stringify(titles);
            console.log("usersfsdbfs1:",user)
            res.render("analysis",{user})
        })
    }else{
        res.render('login',{login:"您还没有登录:请登录"})
    }
})
router.post("/conditions", async function (req, res) {
    var data = req.body;
    var sql2 = "SELECT * FROM analysis_conditions WHERE 标题 = '" + data.text + "'";
    conn.connection2.query(sql2, function (err, result) {
        if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
        }
        res.json({data:result});
    })
})
module.exports = router;